#!/bin/bash

fail_build ()
{
	echo BUILD FAILED
	exit 1
}

rm -rf target
mkdir target

mkdir -p target/generator/classes
cp -R src/generator/xsd/* target/generator/classes
javac -d target/generator/classes `find src/generator/java -name *.java`
if [ $? -ne 0 ]; then
	fail_build
fi

mkdir -p target/generator-test/classes
javac -d target/generator-test/classes -cp target/generator/classes:lib/junit-4.5.jar `find src/generator-test/java -name *.java`
if [ $? -ne 0 ]; then
	fail_build
fi

java -cp target/generator-test/classes:target/generator/classes:lib/junit-4.5.jar org.hypirinha.html.codegen.TestRunner
if [ $? -ne 0 ]; then
	fail_build
fi

java -cp target/generator/classes org.hypirinha.html.codegen.CodeGenerator
if [ $? -ne 0 ]; then
	fail_build
fi

mkdir -p target/main/classes
javac -d target/main/classes `find target/generated/java -name *.java` `find src/main/java -name *.java`
if [ $? -ne 0 ]; then
	fail_build
fi

mkdir -p target/test/classes
javac -d target/test/classes -cp target/main/classes:lib/junit-4.5.jar -encoding utf8 `find src/main-test/java -name *.java`
if [ $? -ne 0 ]; then
	fail_build
fi

java -cp target/test/classes:target/main/classes:lib/junit-4.5.jar org.hypirinha.html.TestRunner
if [ $? -ne 0 ]; then
	fail_build
fi

jar cf target/hypirinha.jar -C target/main/classes .
if [ $? -ne 0 ]; then
	fail_build
fi

jar cf target/hypirinha-src.jar -C src/main/java .
if [ $? -ne 0 ]; then
	fail_build
fi

jar uf target/hypirinha-src.jar -C target/generated/java .
if [ $? -ne 0 ]; then
	fail_build
fi

echo BUILD SUCCESSFUL
exit 0